/**
 * Created by enixjin on 12/22/17.
 */

import {Injectable} from "@angular/core";
import {ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot} from "@angular/router";
import {DialogService} from "./dialogService";

@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
    backURL: string;

    constructor(private router: Router, public _dialogService: DialogService) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        const url: string = state.url;
        return this.checkLogin(url);
    }

    canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        return this.canActivate(route, state);
    }

    checkLogin(url: string): boolean {
        const loginUser = JSON.parse(localStorage.getItem("loginUser"));
        const token = localStorage.getItem("token");
        if (!loginUser || !token) {
            this.backURL = url;
            this._dialogService.pushDialogMsg({showNOButton: false, title: "微信用户绑定", content: "退出系统绑定微信用户", btnOKText: "知道了"})
            // this.router.navigate(["/login"]);
            return false;
        } else {
            return true;
        }
    }

}
